using System;
using System.Drawing;
using System.Windows.Forms;
using FarPoint.Win.Spread.Model;
using FarPoint.Win.Spread.Chart;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.CellType;

namespace Spread.Sparkline.Features
{
    public partial class IntroSparkline : Spread.Common.Features.Sample.BaseSampleControl
    {
        private DateTimeCellType _dateCell;
        private CurrencyCellType _currencyCell;

        public IntroSparkline()
        {
            InitializeComponent();
            Name = "Introduction";

            _dateCell = new DateTimeCellType();
            _dateCell.DateTimeFormat = DateTimeFormat.ShortDate;

            _currencyCell = new CurrencyCellType();
            _currencyCell.DecimalPlaces = 2;

            InitSpreadSheet(fpSpread.ActiveSheet);
            comboType.DataSource = Enum.GetValues(typeof(SparklineType));

            AddSampleSparklines(fpSpread.ActiveSheet);

            txtRow.TextChanged += RowCol_TextChanged;
            txtColumn.TextChanged += RowCol_TextChanged;

            btnAdd.Click += BtnAdd_Click;
            btnClear.Click += BtnClear_Click;

            fpSpread.EnterCell += FpSpread_EnterCell;
        }

        #region Private methods
        private void InitSpreadSheet(SheetView sheet)
        {
            fpSpread.AllowCellOverflow = true;
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 24);
            sheet.SetMultipleColumnWidths(0, sheet.ColumnCount, 75);
        }

        private void AddSampleSparklines(SheetView sheet)
        {
            Font boldFont = new Font("Segoe", 9, FontStyle.Bold);

            sheet.Cells[1, 1].Text = "Stock Analysis example:";
            sheet.Cells[1, 1].Font = boldFont;
            Random random = new Random();
            DateTime date = DateTime.Now;
            for (int row = 2; row < 7; ++row)
            {
                sheet.Cells[row, 1].Value = date.AddDays(random.Next(-10, 10));
                sheet.Cells[row, 1].Font = boldFont;
                sheet.Cells[row, 2].Value = random.Next(50, 60);

                date = date.AddDays(random.Next(-5, -2));
            }
            sheet.Cells[2, 1, 6, 1].CellType = _dateCell;
            sheet.Cells[2, 1, 6, 1].HorizontalAlignment = CellHorizontalAlignment.Right;
            sheet.Cells[2, 2, 6, 2].CellType = _currencyCell;

            sheet.Cells[8, 1].Text = "Line Sparkline";
            sheet.Cells[8, 1].Font = boldFont;
            sheet.Cells[8, 3].Text = "Column Sparkline";
            sheet.Cells[8, 3].Font = boldFont;
            sheet.Cells[8, 5].Text = "Winloss Sparkline";
            sheet.Cells[8, 5].Font = boldFont;

            sheet.Cells[9, 0].Text = "Without Date Axis";
            sheet.Cells[9, 0].Font = boldFont;
            sheet.Cells[9, 0].VerticalAlignment = CellVerticalAlignment.Center;
            sheet.Cells[12, 0].Text = "With Date Axis";
            sheet.Cells[12, 0].Font = boldFont;
            sheet.Cells[12, 0].VerticalAlignment = CellVerticalAlignment.Center;

            sheet.Rows[9].Height = 35;
            sheet.Rows[12].Height = 35;

            sheet.Columns[0].Width = sheet.GetPreferredColumnWidth(0);
            for (int i = 1; i < 7; ++i)
                sheet.Columns[i].Width = 120;

            var data = new SheetCellRange(sheet, 2, 2, 5, 1);

            var setting = new ExcelSparklineSetting();
            var dateAxisSetting = new ExcelSparklineSetting() { DateAxis = true };
            dateAxisSetting.Formula = "$B$3:$B$7";

            sheet.AddSparkline(data, new CellRange(9, 1, 1, 1), SparklineType.Line, setting);
            sheet.AddSparkline(data, new CellRange(9, 3, 1, 1), SparklineType.Column, setting);
            sheet.AddSparkline(data, new CellRange(9, 5, 1, 1), SparklineType.Winloss, setting);

            sheet.AddSparkline(data, new CellRange(12, 1, 1, 1), SparklineType.Line, dateAxisSetting);
            sheet.AddSparkline(data, new CellRange(12, 3, 1, 1), SparklineType.Column, dateAxisSetting);
            sheet.AddSparkline(data, new CellRange(12, 5, 1, 1), SparklineType.Winloss, dateAxisSetting);
        }

        private void TryToggleButtonState()
        {
            int val = 0;
            btnAdd.Enabled = (int.TryParse(txtRow.Text, out val) && int.TryParse(txtColumn.Text, out val));
        }
        #endregion

        #region Event handlers
        private void FpSpread_EnterCell(object sender, EnterCellEventArgs e)
        {
            var sparkline = fpSpread.ActiveSheet.GetSparkline(fpSpread.ActiveSheet.ActiveRowIndex,
                fpSpread.ActiveSheet.ActiveColumnIndex);
            btnClear.Enabled = (sparkline != null);
            comboType.Enabled = (sparkline == null);

            if (sparkline == null)
                TryToggleButtonState();
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            SheetView sheet = fpSpread.ActiveSheet;
            sheet.SparklineContainer.RemoveSparkline(sheet.ActiveRowIndex, sheet.ActiveColumnIndex);
            btnClear.Enabled = false;
            TryToggleButtonState();
            comboType.Enabled = true;
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            SheetView sheet = fpSpread.ActiveSheet;
            CellRange range = sheet.GetSelection(0);

            int row = int.Parse(txtRow.Text);
            int col = int.Parse(txtColumn.Text);
            SparklineType sparklineType = (SparklineType)comboType.SelectedItem;
            
            if (range.IsValidRange(sheet))
            {
                ExcelSparklineSetting setting = new ExcelSparklineSetting();
                int dataRow = range.Row;
                if (range.RowCount == 2)
                {
                    setting.DateAxis = true;
                    setting.Formula = $"${(char)('A' + range.Column)}${range.Row + 1}:${(char)('A' + range.Column + range.ColumnCount - 1)}" +
                        $"${range.Row + range.RowCount - 1}";

                    dataRow = range.Row + 1;
                }
                
                try
                {
                    sheet.AddSparkline(new SheetCellRange(sheet, dataRow, range.Column, 1, range.ColumnCount), 
                        new CellRange(row, col, 1, 1), sparklineType, setting);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error adding Sparkline", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void RowCol_TextChanged(object sender, EventArgs e)
        {
            TryToggleButtonState();
        }
        #endregion
    }
}


//
//IntroSparkline.Designer.cs
//


namespace Spread.Sparkline.Features
{
    partial class IntroSparkline
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(IntroSparkline));
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.txtRow = new System.Windows.Forms.TextBox();
            this.txtColumn = new System.Windows.Forms.TextBox();
            this.comboType = new System.Windows.Forms.ComboBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            this.pnlMain.Size = new System.Drawing.Size(477, 703);
            // 
            // pnlControls
            // 
            this.pnlControls.AutoScroll = true;
            this.pnlControls.Controls.Add(this.comboType);
            this.pnlControls.Controls.Add(this.txtColumn);
            this.pnlControls.Controls.Add(this.txtRow);
            this.pnlControls.Controls.Add(this.btnClear);
            this.pnlControls.Controls.Add(this.btnAdd);
            this.pnlControls.Controls.Add(this.label4);
            this.pnlControls.Controls.Add(this.label3);
            this.pnlControls.Controls.Add(this.label2);
            this.pnlControls.Controls.Add(this.panel1);
            this.pnlControls.Size = new System.Drawing.Size(157, 703);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 703);
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.LightGray;
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(157, 139);
            this.panel1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(157, 139);
            this.label1.TabIndex = 0;
            this.label1.Text = resources.GetString("label1.Text");
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(3, 160);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Row:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(3, 190);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(45, 13);
            this.label3.TabIndex = 2;
            this.label3.Text = "Column:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(3, 220);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(34, 13);
            this.label4.TabIndex = 2;
            this.label4.Text = "Type:";
            // 
            // btnAdd
            // 
            this.btnAdd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnAdd.Enabled = false;
            this.btnAdd.Location = new System.Drawing.Point(33, 256);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(94, 25);
            this.btnAdd.TabIndex = 3;
            this.btnAdd.Text = "Add Sparkline";
            this.btnAdd.UseVisualStyleBackColor = true;
            // 
            // btnClear
            // 
            this.btnClear.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnClear.Enabled = false;
            this.btnClear.Location = new System.Drawing.Point(33, 289);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(94, 25);
            this.btnClear.TabIndex = 4;
            this.btnClear.Text = "Clear Sparkline";
            this.btnClear.UseVisualStyleBackColor = true;
            // 
            // txtRow
            // 
            this.txtRow.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtRow.Location = new System.Drawing.Point(71, 157);
            this.txtRow.Name = "txtRow";
            this.txtRow.Size = new System.Drawing.Size(83, 20);
            this.txtRow.TabIndex = 0;
            // 
            // txtColumn
            // 
            this.txtColumn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtColumn.Location = new System.Drawing.Point(71, 187);
            this.txtColumn.Name = "txtColumn";
            this.txtColumn.Size = new System.Drawing.Size(83, 20);
            this.txtColumn.TabIndex = 1;
            // 
            // comboType
            // 
            this.comboType.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboType.FormattingEnabled = true;
            this.comboType.Location = new System.Drawing.Point(71, 217);
            this.comboType.Name = "comboType";
            this.comboType.Size = new System.Drawing.Size(83, 21);
            this.comboType.TabIndex = 2;
            // 
            // IntroSparkline
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "IntroSparkline";
            this.Size = new System.Drawing.Size(635, 703);
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            this.pnlControls.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboType;
        private System.Windows.Forms.TextBox txtColumn;
        private System.Windows.Forms.TextBox txtRow;
        private System.Windows.Forms.Button btnClear;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
    }
}
